<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;
use app\app\util\IntUtils;
class  MatchRoomController extends BaseController {


    /**
     * @return array
     * [ 匹配模式 创建房间 ]
     */
    public function room()
    {
        //ft_match_room表插入数据，创建房间
        $user_id =  OnlineUtils::getUserIdBySid();           //邀请人id
        $match_room['user_id'] = $user_id;
        $match_room['created_at'] = time();
        $id = DB::name('match_room')->insertGetId($match_room);  //房间id
        return ft(200,'success',['user_id'=>$user_id,'room_id'=>$id]);
    }


    /**
     * @return array
     * [ 创建房间，邀请好友 ]
     */
    public function invitedFriend()
    {
        $match_room_id = input('post.match_room_id');
        $master_user_id = input('post.user_id');
        $user_id = OnlineUtils::getUserIdBySid();
        $master_user = Db::name('user')->find($master_user_id);
        $user = Db::name('user')->find($user_id);
        return wc(200,'success',['room_id'=>$match_room_id,'master_user'=>$master_user,'user'=>$user]);
    }
    /**
     * @return array
     * [ 游戏对战信息 ]
     */
    public function matchFight()
    {
        $fight['match_room_id'] = input('post.match_room_id')?input('post.match_room_id'):0; //房间号
        $fight['user_id'] =  OnlineUtils::getUserIdBySid();
        $fight['my_score'] = input('post.my_score')?input('post.my_score'):0;
        $fight['other_name'] = input('post.other_name')?input('post.other_name'):0;
        $fight['other_score'] = input('post.other_score')?input('post.other_score'):0;
        $fight['result'] = input('post.result')?input('post.result'):0;
        $fight['created_at'] = time();
        //插入数据
        if( DB::name('match_fight')->insertGetId($fight) ){
            //游戏记录生成后，就在user的游戏次数加1
            Db::query('update ft_user set times = times + 1 where id ='.$fight['user_id']);
            //如果用户获胜在用户表（ft_user）中给该用户的积分加5，如果用户失败，在用户表（ft_user）中给该用户的积分加1
            if( $fight['result'] == 1 ){
                Db::query('update ft_user set integral = integral + 5 ,total_integral = total_integral + 5, victory_times = victory_times + 1 where id ='.$fight['user_id']);
                IntUtils::getUserInt($fight['user_id'],2);
            } elseif ( $fight['result'] == 2 ){
                Db::query('update ft_user set integral = integral + 1,total_integral = total_integral + 1 where id ='.$fight['user_id']);
                IntUtils::getUserInt($fight['user_id'],1);
            }
            return ft(200,'插入数据成功',$fight);
        }
    }

    //人机对战返回假数据
    public function ManMachine(){
        $id   = rand(1,12);
        $data = Db::name('card')->field('id,name,concat("'.config('IMG_HEAD').'",img) avatar_url
        ')->where('id','eq',$id)->find();
        return ft(200,'success',['data'=>$data]);
    }

    /**
     * @return array
     * 游戏记录
     */
    public function GameRecord()
    {
        $user_id =  OnlineUtils::getUserIdBySid();
        $page = input('post.page');
        if( $page <= 0 ){
            $length = 0;
        }else{
            $length = 5*$page;
        }
        $record = Db::name('match_fight')->where('user_id','eq',$user_id)->limit($length,5)->order('id desc')->select();
        foreach ($record as $k => $v){
            $record[$k]['created_at'] = date('Y-m-s H:i:s',$v['created_at']);
        }
        return ft(200,'success',$record);

    }

}